﻿using System;
using Microsoft.Win32;

namespace PocketGPW
{
    internal static class SettingsHelper
    {
        private const string RegistryKeyName = "Software\\PocketGPW";

        private const int DefaultRefresh = 2;
        private const bool DefaultNotifications = true;

        internal enum Keys
        {
            Refresh,
            Notifications
        }

        internal static int GetInt32(Keys key)
        {
            // registryKey
            RegistryKey registryKey;

            try
            {
                registryKey = Registry.LocalMachine.OpenSubKey(RegistryKeyName);
            }
            catch
            {
                if (key == Keys.Refresh)
                    return DefaultRefresh;
                else
                    return Int32.MaxValue;
            }

            if (registryKey == null)
            {
                if (key == Keys.Refresh)
                    return DefaultRefresh;
                else
                    return Int32.MaxValue;
            }

            // value
            object value = null;

            try
            {
                value = registryKey.GetValue(key.ToString());
            }
            catch { }

            if (value == null || value.ToString().Length == 0)
            {
                if (key == Keys.Refresh)
                    return DefaultRefresh;
                else
                    return Int32.MaxValue;
            }

            return Convert.ToInt32(value);
        }

        internal static bool GetBoolean(Keys key)
        {
            // registryKey
            RegistryKey registryKey;

            try
            {
                registryKey = Registry.LocalMachine.OpenSubKey(RegistryKeyName);
            }
            catch
            {
                if (key == Keys.Notifications)
                    return DefaultNotifications;
                else
                    return false;
            }

            if (registryKey == null)
            {
                if (key == Keys.Notifications)
                    return DefaultNotifications;
                else
                    return false;
            }

            // value
            object value = null;

            try
            {
                value = registryKey.GetValue(key.ToString());
            }
            catch { }

            if (value == null || value.ToString().Length == 0)
            {
                if (key == Keys.Notifications)
                    return DefaultNotifications;
                else
                    return false;
            }

            return Convert.ToBoolean(value);
        }

        internal static void Set(Keys key, int value)
        {
            RegistryKey registryKey;
            try
            {
                registryKey = Registry.LocalMachine.CreateSubKey(RegistryKeyName);
            }
            catch
            {
                return;
            }

            if (registryKey == null)
                return;

            registryKey.SetValue(key.ToString(), value);
        }

        internal static void Set(Keys key, bool value)
        {
            RegistryKey registryKey;
            try
            {
                registryKey = Registry.LocalMachine.CreateSubKey(RegistryKeyName);
            }
            catch
            {
                return;
            }

            if (registryKey == null)
                return;

            registryKey.SetValue(key.ToString(), value);
        }
    }
}
